# -*- coding: ascii -*-
import sys
import numpy as np
from PyQt4 import QtCore, QtGui, uic
import gen as g
from gen import t as t

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        uic.loadUi('.\gui\main.ui', self)
        self.p = g.gen()
    ###########################################################################
    #                         Коннекты
    ###########################################################################
        self.connect(self.start_but, QtCore.SIGNAL("clicked()"),
                     self.raschet)
        con_confines = lambda n: self.connect(n, QtCore.SIGNAL('valueChanged(double)'),
                     self.n_confines)
        con_coun = lambda n: self.connect(n, QtCore.SIGNAL('valueChanged(double)'),
                     self.n_coun)
        con_characteristics = lambda n: self.connect(n, QtCore.SIGNAL('valueChanged(double)'),
                     self.set_characteristics)
        con_size = lambda n: self.connect(n, QtCore.SIGNAL('valueChanged(double)'),
                     self.n_size)
    ###########################################################################
    #                         Реакции
    ###########################################################################
        con_confines(self.ogr1min)
        con_confines(self.ogr1max)
        con_confines(self.ogr2min)
        con_confines(self.ogr2max)
        con_confines(self.ogr3min)
        con_confines(self.ogr3max)

        con_coun(self.n_stop)
        con_coun(self.max_gen)

        self.bar_stop.setValue(0)
        self.bar_pok.setValue(0)
    ###########################################################################
    #                         Установка умолчаний
    ###########################################################################
        self.n_confines()
        self.n_coun()
        self.n_size()
        self.set_characteristics()

    ###########################################################################
    #                         Реакторы
    ###########################################################################
    def n_confines(self):
        self.ogr1min.setValue(min(self.ogr1min.value(), self.ogr1max.value()))
        self.ogr1max.setValue(max(self.ogr1min.value(), self.ogr1max.value()))

        self.ogr2min.setValue(min(self.ogr2min.value(), self.ogr2max.value()))
        self.ogr2max.setValue(max(self.ogr2min.value(), self.ogr2max.value()))

        self.ogr3min.setValue(min(self.ogr3min.value(), self.ogr3max.value()))
        self.ogr3max.setValue(max(self.ogr3min.value(), self.ogr3max.value()))

        self.set_confines()

    def n_coun(self):
        self.set_coun(self.n_stop.value(), self.max_gen.value())


    def n_size(self):
        self.set_size(p_size=self.pop_size.value())

    ###########################################################################
    #                         Установка параметров
    ###########################################################################
    def set_confines(self):
        confines =  np.array([
                        [self.ogr1min.value(),self.ogr1max.value()],
                        [self.ogr2min.value(),self.ogr2max.value()],
                        [self.ogr3min.value(),self.ogr3max.value()]])
        self.p.set_confines(confines)

    def set_characteristics(self):
        self.p.soil_characteristics(b=1.5, phi=10, sc=10, gam=10, q=10)
        self.textBrowser.append("set_characteristics")

    def set_size(self, p_size, n_elit=0.1, n_selection=0.1):
        self.p.p_size(p_size, n_elit, n_selection)
        self.textBrowser.append("set_size")

    def set_coun(self, n_stop, max_gen):
        self.p.set_gen_stop(n_stop, max_gen)
        self.bar_stop.setRange(0,n_stop)
        self.bar_pok.setRange(0,max_gen)
        self.textBrowser.append("set_coun")
    ###########################################################################
    #                         Расчёт и отрисовка
    ###########################################################################
    def upd(self, N, i, stop, text = ""):
        self.min_N.display(N)
        self.bar_pok.setValue(i)
        self.bar_stop.setValue(stop)
        if text != "": self.textBrowser.append(text)

    def raschet(self):
#        if self.p.generation > 2:
        self.p.purge()
        self.upd(self.p.min_fitness, self.p.generation, self.p.stop, t("Конец"))
#        n_elit, n_selection = 0.1, 0.1
#        population, array, minmax, rise = self.p.iteration_1()
#        self.upd(self.p.min_fitness, self.p.generation, self.p.stop, t("Расчёт запущен"))
#        while self.p.stop < self.p.n_stop and self.p.generation < self.p.max_gen:
#            self.p.p_size(0, n_elit, n_selection)
#            population, array, minmax, rise = self.p.iteration_n(population)
#            if round(rise) == 0 or self.p.percent_bed > 80:
#                n_elit = 0.2
#                n_selection = 0.5
#                self.p.mutation += 0.2
#                text = ""
#            else:
#                n_elit = 0.1
#                n_selection = 0.7
#                self.p.mutation -= 0.2
#                text = 'i = %d rise = %d perc = %d' % (self.p.generation, round(rise), self.p.percent_bed)
#                print(text)
#            if abs(self.p.mutation) > 3 or self.p.percent_bed > 90:
#                n_elit = 0.2
#                n_selection = 0.1
#                self.p.mutation = 0.25
#                text = '!!! i = %d rise = %d perc = %d - REBILD' % (self.p.generation, round(rise), self.p.percent_bed)
#            self.upd(self.p.min_fitness, self.p.generation, self.p.stop, text)
#        best = self.p.best_var
#        self.ogr1min.setValue(best[0] * 0.5)
#        self.ogr1max.setValue(best[0] * 2)
#        self.ogr2min.setValue(best[1] * 0.5)
#        self.ogr2max.setValue(best[1] * 2)
#        self.ogr3min.setValue(best[2] * 0.5)
#        self.ogr3max.setValue(best[2] * 2)
#        self.upd(self.p.min_fitness, self.p.generation, self.p.stop, t("Конец"))

app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
